package com.qiang.test14.Dao;

import com.qiang.test14.domain.Teacher;

import java.util.ArrayList;

public class TeacherImpDao implements IteacherDao {

    static ArrayList<Teacher> list = new ArrayList<Teacher>();

    @Override
    public void addTeacher(Teacher teacher) {
        list.add(teacher);

    }

    @Override
    public void deleteTeacher(String id) {

        //根据索引删除
        int index = getIndex(id);
        list.remove(index);

    }

    @Override
    public void updateTeacher(String id, Teacher nerteacher) {

        //根据索引修改

        int index = getIndex(id);
        list.set(index, nerteacher);
    }

    private int getIndex(String id) {
        //遍历集合，根据id找索引
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null && id.equals(list.get(i).getId()))
                index = i;
        }
        return index;
    }

    @Override
    public ArrayList<Teacher> findTeacher() {
        return list;

    }
}
